package com.example.pageexample.database;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.paging.PageKeyedDataSource;
import androidx.room.DatabaseConfiguration;
import androidx.room.InvalidationTracker;
import androidx.sqlite.db.SupportSQLiteOpenHelper;

import com.example.pageexample.PageApplication;
import com.example.pageexample.page.Constants;
import com.example.pageexample.page.LoadCallback;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * 数据库操作工具类
 * @author mailanglideguozhe
 * @date 20210703
 */
public class PictureHelper {
    private static PictureDao dao = null;
    private static PictureHelper helper;

    private PictureHelper() {
    }

    public static PictureHelper getInstance(){
        if(null == helper){
            synchronized(PictureHelper.class){
                if(null == helper){
                    helper = new PictureHelper();
                    dao = BaseDatabase.getInstance(PageApplication.getInstance()).pictureDao();
                }
            }
        }
        return helper;
    }



    /**
     * 插入单条数据图片，并主动通知当前数量
     * */
    public void insert(Picture picture){
        Schedulers.computation().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                dao.insert(picture);
                if(null != mEmitter){
                    mEmitter.onNext(getNumOfPicture());
                }
            }
        });
    }

    /**
     * 插入数据列表
     * @param list
     */
    public void insertAll(List<Picture> list){
        Schedulers.computation().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                dao.insertAll(list);
                if(null != mEmitter){
                    mEmitter.onNext(getNumOfPicture());
                }
            }
        });
    }

    /**
     * 获取已有图片数量
     * */
    public int getNumOfPicture(){
        int count = dao.getCount();
        return count;
    }

    /***
     * 获取图片
     * @param key 传入下一页的页码
     */
    public void getData(int pagePosition , int count , PageKeyedDataSource.LoadInitialCallback<Integer, Picture> callback , int key){
        List<Picture> pictureList = dao.getData(pagePosition * Constants.PAGE_SIZE,count);
        callback.onResult(pictureList,0,key);
    }

    public void getData(int pagePosition , int count , PageKeyedDataSource.LoadCallback<Integer, Picture> callback , int key){
        List<Picture> pictureList = dao.getData(pagePosition * Constants.PAGE_SIZE,count);
        callback.onResult(pictureList,key);
    }

    /**
     * 判断数据库是否有下一页
     * @param page
     * @return
     */
    public boolean hasNextPage(int page){
        int count = dao.getCount();
        if(page * Constants.PAGE_SIZE < count){
            return true;
        }
        return false;
    }

    private ObservableEmitter<Integer> mEmitter;
    /**
     * 请求计算列表总长度
     */
    public Observable<Integer> requestTotalCount(){
        Observable<Integer> observable = io.reactivex.Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                PictureHelper.this.mEmitter = emitter;
                //子线程请求列表总长度
                Schedulers.computation().scheduleDirect(new Runnable() {
                    @Override
                    public void run() {
                        int count = dao.getCount();
                        mEmitter.onNext(count);
                    }
                });
            }
        });
        return observable;
    }
}
